<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Controls actions related to the shows dialog
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 // Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access');

// Controller
jimport('joomla.application.component.controller');

// Make sure this user is a registered one. Otherwise, send him to the registration page
require_once(JPATH_COMPONENT.DS.'models'.DS.'user.php');

class MusashiControllerShows extends JController
{

    
    /**
     * Displays the main Costumes view
     * @access public
     */
    function display()
    {
        // Make sure we're registered  users
        if( MusashiModelUser::validateUser( 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ) == false )
        {
            // Redirect to registration view
            $url = MusashiModelUser::getUnregisteredUserPath();
            $this->setRedirect( $url );            
        }

        parent::display();
    }
  
  /**
   *    Adds the basic information for a new show
   */
  function newShow()
  {
    $model    =& $this->getModel('shows');
    $name     = JRequest::getString('name');
    $datetime = JRequest::getString('datetime');
    $route    = JRoute::_('index.php?option=com_musashi&controller=shows&view=shows',false);
    
    // Add this new show
    $result   = $model->addShow($name,$datetime);
    
    if( $result )
    {
        $route .= '&sid='.$result;
    }
    else
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to create new show!');
    }
    
    
    $this->setRedirect($route);

  }
  
  /**
   *    Modify the basic information about a given show
   */
  function updateShow()
  {
    $model    =& $this->getModel('shows');
    $datetime = JRequest::getString('datetime');
    $contact  = JRequest::getString('contact');    
    $notes    = JRequest::getString('notes');
    $status   = JRequest::getInt('status');
    $pay      = JRequest::getFloat('pay');
    $sID      = JRequest::getInt('sid');
    
    // Update this show
    if ( $model->updateShowBasic($sID,$datetime,$contact,$notes,$status,$pay) == false )
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to update show!');        
    }

  }
  
  /**
   *    Adds a new venue, and registers it as the designated one for the current
   *    show
   */
  function addVenue()
  {
    $model    =& $this->getModel('shows');
    $name        = JRequest::getString('name');
    $description = JRequest::getString('description');
    $address     = JRequest::getString('address');
    $contact     = JRequest::getString('contact');
    $notes       = JRequest::getString('notes');
    $link        = JRequest::getString('link');
    $sID         = JRequest::getInt('sid');
    $route       = JRoute::_('index.php?option=com_musashi&controller=shows&view=shows&sid='.$sID.'#venue',false);
    
    // First, register the new venue
    $venueID = $model->addVenue($name,$description,$address,$contact,$notes,$link);
    
    // If sucessful, register
    if($venueID)
    {
        if(!$model->setShowVenue($sID, $venueID))
        {
            $session =& JFactory::getSession();
            $session->set('errorMessage','Failed to register venue for this show!');                            
        }
    }
    else
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to add venue!');                
    }
    
    $this->setRedirect($route);            
    
  }
  
  /**
   *    Update the notes regarding a given venue
   */
  function updateVenueComments()
  {
    $model  =& $this->getModel('shows');
    $vID    = JRequest::getString('vid');
    $notes  = JRequest::getString('notes');
    $sID    = JRequest::getInt('sid');
    
    if( $model->updateVenueNotes($vID, $notes) == false )
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to update venue comments!');                                    
    }
    
  }
  
  /**
   *    Updates the venue iD of a show
   */
  function updateVenue()
  {
    $model  =& $this->getModel('shows');
    $vID    = JRequest::getString('vid');
    $sID    = JRequest::getInt('sid');
    
    if( $model->setShowVenue($sID, $vID) == false )
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to update venue!');                                            
    }
    
  }
  
  /**
   *    Updates an user's role in a show
   */
  function updateParticipation()
  {
    $model  =& $this->getModel('shows');
    $showID  = JRequest::getInt('sid');
    $usrID   = JRequest::getInt('mid');
    $roleID  = JRequest::getInt('role');
    $statID  = JRequest::getInt('status');
    
    if( $model->setMemberRole($showID, $usrID, $roleID, $statID ) == false )
    {
        $session =& JFactory::getSession();
        $session->set('errorMessage','Failed to update user!');                                                    
    }
    
    $route  = JRoute::_('index.php?option=com_musashi&controller=shows&view=shows&sid='.$showID.'#members',false);    
    $this->setRedirect($route);            
  }
  
    
}

?>